package org.ourgrid.broker.business.schedulerdata.executor;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.broker.business.schedulerdata.SchedulerData;
import org.ourgrid.broker.response.StartWorkResponseTO;
import org.ourgrid.common.internal.IResponseTO;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class StartWorkExecutor extends AbstractSchedulerDataExecutor {
	
	/**
	 * <START_WORK workerID='String' requestID='long' jobID='int' taskID='int' processID='int'/> 
	 */
	public List<IResponseTO> execute(SchedulerData command) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		Document logDocFile = super.getXMl(command.getXml());
		
		Element element = logDocFile.getDocumentElement();
		
		long requestID = Long.valueOf(element.getAttribute("requestID"));
		String workerID = element.getAttribute("workerID");
		int jobID = Integer.valueOf(element.getAttribute("jobID"));
		int taskID = Integer.valueOf(element.getAttribute("taskID"));
		int processID = Integer.valueOf(element.getAttribute("processID"));
		
		StartWorkResponseTO to = new StartWorkResponseTO();
		to.setJobID(jobID);
		to.setProcessID(processID);
		to.setRequestID(requestID);
		to.setTaskID(taskID);
		to.setWorkerAddress(getAddress(workerID));
		
		responses.add(to);
		
		return responses;
	}
}
